﻿<cfsilent>

	<cfscript>

		propAdvice = getProperty("serviceFactory").getBean("timeTablePropertyAdvice");
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		response = structNew();
		
		transaction {
			
			weekDay = event.getArg("wkday");
			lectureId = event.getArg("lec");
		
			usageAdvice = propAdvice.init();
		
			sql = "SELECT a.props 
					FROM t_timetable a 
					WHERE a.wdidx = :WeekDay 
						  AND 
						  a.lecidx = :LectureIndex ";
						
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="WeekDay", value=weekDay, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="LectureIndex", value=lectureId, cfsqltype="cf_sql_char" );
			
			rs_tableSet = queryObj.execute( sql=sql ).getResult();
			
			if (rs_tableSet.recordCount EQ 0) {
			
				sql = "INSERT INTO t_timetable (
							   props,
							   wdidx,
							   lecidx
						) VALUES (
						  	   :WeekDay,
							   :LectureIndex,
							   :UsageProps
						) ";
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="WeekDay", value=weekDay, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="LectureIndex", value=lectureId, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="UsageProps", value=usageAdvice.stringProperty(), cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
			
			}
			else {
				usageAdvice.parseProperty( rs_tableSet.props );
			}
			
			switch (event.getArg('method')) {
				
				case "FinalExam":
					/* 期末考试可用时间 */
					if (yesNoFormat(event.getArg('status'))) {
						usageAdvice.setFinalExam(true);
					}
					else {
						usageAdvice.setFinalExam(false);
					}
					break;
				
				case "HalfExam":
					/* 半期考试可用时间 */
					if (yesNoFormat(event.getArg('status'))) {
						usageAdvice.setHalfExam(true);
					}
					else {
						usageAdvice.setHalfExam(false);
					}
					break;
				
				case "DisperseExam":
					/* 自助分散考试可用时间 */
					if (yesNoFormat(event.getArg('status'))) {
						usageAdvice.setDisperseExam(true);
					}
					else {
						usageAdvice.setDisperseExam(false);
					}
					break;
				
				case "ResitExam":
					/* 补考可用时间 */
					if (yesNoFormat(event.getArg('status'))) {
						usageAdvice.setResitExam(true);
					}
					else {
						usageAdvice.setResitExam(false);
					}
					break;
					
					
					
			}
		
			sql = "UPDATE t_timetable a 
					SET a.props = :UsageProps 
					WHERE a.wdidx = :WeekDay 
						  AND 
						  a.lecidx = :LectureIndex ";
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="WeekDay", value=weekDay, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="LectureIndex", value=lectureId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="UsageProps", value=usageAdvice.stringProperty(), cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
							
			structInsert(response, "FinalExam", usageAdvice.getFinalExam());
			structInsert(response, "HalfExam", usageAdvice.getHalfExam());
			structInsert(response, "DisperseExam", usageAdvice.getDisperseExam());
			structInsert(response, "ResitExam", usageAdvice.getResitExam());

		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>